/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx179labs.nez.hr.dao.file;

import javafx179labs.nez.hr.bean.Post;
import javafx179labs.nez.hr.bean.Task;
import java.util.ArrayList;
import java.util.List;
import javafx179labs.nez.hr.bean.Department;
import javafx179labs.nez.hr.bean.Employe;

/**
 *
 * @author SSheypak
 */
public class HRDaoFileImpl implements HRDao{
    //TODO: parametrize this read about Properties

    private static String path;
    private static String departmentsDB;
    private static String employesDB;
    private static String postsDB;
    private static String tasksDB;


    List<Department> departments = new ArrayList<Department>();
    List<Task> tasks = new ArrayList<Task>();
    List<Employe> employes = new ArrayList<Employe>();
    List<Post> posts = new ArrayList<Post>();

    public static void setPath(String path) {
        HRDaoFileImpl.path = path;
    }

    public HRDaoFileImpl() {
        //Bad
        HRDaoFileImpl.departmentsDB = path + "departments.txt";
        HRDaoFileImpl.employesDB = path + "employes.txt";
        HRDaoFileImpl.postsDB = path + "posts.txt";
        HRDaoFileImpl.tasksDB = path + "tasks.txt";
    }

    //hide it from other objects inside your class
    private static HRDaoFileImpl instance = null;

    //TODO: Read about singleton in Java 1.5 - 1.6 and refactor
    public static HRDao getInstance() {
        if(instance == null){
            instance = new HRDaoFileImpl();
        }
        return instance;
    }

    @Override
    //TODO: synchoronize changes in loaded data and file data
    public List<Department> getDepartments() {
        List<String> lines = new DataReaderHelper(departmentsDB).getLines();
        if (lines !=null && lines.size() > 0) {
            for (String line : lines) {
                String[] splitted = line.split(":::");
                departments.add(new Department(Integer.valueOf(splitted[0].trim()), splitted[1]));
            }
        }
        return departments;
    }

    @Override
    public List<Employe> getEmployes() {
        List<String> lines = new DataReaderHelper(employesDB).getLines();
        if (lines != null && lines.size() > 0) {
            for (String line : lines) {
                String[] splitted = line.split(":::");
                employes.add(new Employe(Integer.valueOf(splitted[0].trim()),
                                         splitted[1].trim(),
                                         splitted[2].trim(),
                                         Integer.valueOf(splitted[3].trim()),
                                         Integer.valueOf(splitted[4].trim())));
            }
        }
        return employes;
    }

    @Override
    public List<Post> getPosts() {
        List<String> lines = new DataReaderHelper(postsDB).getLines();
        if (lines != null && lines.size() > 0) {
            for (String line : lines) {
                String[] splitted = line.split(":::");
                posts.add(new Post(Integer.valueOf(splitted[0].trim()), splitted[1]));

            }
        }
        return posts;
    }

    @Override
    public List<Task> getTasks() {
        List<String> lines = new DataReaderHelper(tasksDB).getLines();
        Task task;
        if (lines != null && lines.size() > 0) {
            for (String line : lines) {
                String[] splitted = line.split(":::");
                task = new Task(Integer.valueOf(splitted[0].trim()), splitted[1]);

                if (!(splitted[2].trim()).equalsIgnoreCase("null")) {
                    task.setPerformer(Integer.valueOf(splitted[2].trim()));
                }
                
                tasks.add(task);
            }
        }
        return tasks;
    }

    @Override
    public void setDepartments(List<Department> list) {
        List<String> lines = new ArrayList<String>();
        for (Department department : list) {
            lines.add(" " + department.getId().toString() + " :::" + department.getName() + ":::\n");
        }
        new DataWriterHelper(departmentsDB, lines);
    }

    @Override
    public void setEmployes(List<Employe> list) {
        List<String> lines = new ArrayList<String>();
        for (Employe employe : list) {
            lines.add(" " + employe.getId().toString() + " ::: "
                      + employe.getFirstName() + " ::: "
                      + employe.getLastName() + " ::: "
                      + employe.getDepartmentId().toString() + " ::: "
                      + employe.getPostId().toString() + " :::\n");
        }
        new DataWriterHelper(employesDB, lines);
    }

    @Override
    public void setPosts(List<Post> list) {
        List<String> lines = new ArrayList<String>();
        for (Post post : list) {
            lines.add(" " + post.getId().toString() + " :::" + post.getName() + ":::\n");
        }
        new DataWriterHelper(postsDB, lines);
    }

    @Override
    public void setTasks(List<Task> list) {
        List<String> lines = new ArrayList<String>();
        for (Task task : list) {
            lines.add(" " + task.getId().toString() + " :::" + task.getTask() + "::: "
                      + ((task.hasPerformer()) ? task.getPerformer().toString() : "null")
                      + " :::\n");
        }
        new DataWriterHelper(tasksDB, lines);
    }
}
